<template>
  <div v-title data-title="博客-blog">
    <el-container>
      <el-main class="me-articles">
        <ArticleScrollPage></ArticleScrollPage>
      </el-main>
      <el-aside>
        <CardMe></CardMe>
        <card-tag :tags="hotTags"></card-tag>
        <card-article
          cardHeader="最新文章"
          :articles="hotArticles"
        ></card-article>
        <card-archive cardHeader="文章归档" :archives="archives"></card-archive>
        <card-article
          cardHeader="最新文章"
          :articles="newArticles"
        ></card-article>
      </el-aside>
    </el-container>
  </div>
</template>

<script>
import ArticleScrollPage from "@/components/common/ArticleScrollPage.vue";
import CardMe from "@/components/card/CardMe.vue";
import CardTag from "@/components/card/CardTag.vue";
import CardArticle from "@/components/card/CardArticle.vue";
import CardArchive from "@/components/card/CardArchive.vue";
import { getHotTags } from "@/api/tag";
import { getNewArticles, getHotArticles, getArchives } from "@/api/article";

export default {
  name: "HomeIndex",
  data() {
    return {
      hotTags: [],
      newArticles: [],
      hotArticles: [],
      archives: [],
    };
  },
  created() {
    this.getHotTags();
    this.getHotArticles();
    this.getNewArticles();
  },
  methods: {
    getHotTags() {
      getHotTags()
        .then((res) => {
          if (res.data.success) {
            this.hotTags = res.data.data;
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch(() => {
          this.$message.error("系统错误");
        })
        .finally(() => {});
    },
    getHotArticles() {
      getHotArticles()
        .then((res) => {
          if (res.data.success) {
            this.hotArticles = res.data.data;
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch(() => {
          this.$message.error("系统错误");
        })
        .finally(() => {});
    },
    getNewArticles() {
      getNewArticles()
        .then((res) => {
          if (res.data.success) {
            this.newArticles = res.data.data;
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch(() => {
          this.$message.error("系统错误");
        })
        .finally(() => {});
    },
    getArchives() {
      getArchives()
        .then((res) => {
          if (res.data.success) {
            this.archives = res.data.data;
          } else {
            this.$message.error(res.data.res);
          }
        })
        .catch(() => {
          this.$message.error("系统异常");
        })
        .finally(() => {});
    },
  },
  components: {
    ArticleScrollPage,
    CardMe,
    "card-tag": CardTag,
    "card-article": CardArticle,
    "card-archive": CardArchive,
  },
};
</script>


<style scoped>
.el-container {
  width: 960px;
}

.el-aside {
  margin-left: 20px;
}

.el-main {
  padding: 0px;
  line-height: 16px;
}

.el-card {
  border-radius: 0;
}

.el-card:not(:first-child) {
  margin-top: 20px;
}
</style>